
package Sgdb::FileEditPane ;

use Sgdb::Utils qw( :DEFAULT
                    ) ;

use warnings ;
use strict ;
use integer ;
use bytes ;

sub new {

    my ($self, $frame, $file, $calback) = @_ ;

    my $t = $frame -> Scrolled ( 'SuperText',
                                 -width => 90,
                                 pref 'file-edit-pane-text',
                                 ) ;
    pref_reconfigure 'file-edit-pane-text' => $t ;

    if (open FILE, $file ) {

        $t -> insert( 'end',
                      join '', <FILE>
                      ) ;
        close FILE ;
    }
    else {

        $t -> insert( 'end',
                      "can't open $file: $!\n"
                      ) ;
    }

    my $bg = $t -> cget( '-background' ) ;

    $frame -> configure ( -background => $bg ) ;

    my $f = $frame -> Frame( -background => $bg ,
                             ) -> pack ( -fill => 'x',
                                         -pady => 0,
                                         ) ;

    my @button_pref = ( -width => 10, pref 'file-edit-button' ) ;

    pref_reconfigure 'file-edit-button',
    $f -> Button ( -text => 'O.K./Save',
                   -command => sub {
                       $calback -> ( save_edited_file_as( $t, $file) ) ;
                       # Perlon: in the callback, a command like:
                       #          hook_run 'close-current-source-pane' ;
                       # will close the current window
                   },
                   @button_pref,
                   ) -> pack ( -side => 'left',
                               -anchor => 'w',
                               -padx => 5,
                               -pady => 0,
                               ) ;

    pref_reconfigure 'file-edit-button',
    $f -> Button ( -text => 'Cancel/Close',
                   -command => [\&hook_run, 'close-current-source-pane'],
                   @button_pref,
                   ) -> pack ( -side => 'left',
                               -anchor => 'w',
                               -padx => 5,
                               -pady => 0,
                               ) ;

    pref_reconfigure 'file-edit-button',
    $f -> Button ( -text => 'Save As',
                   -command => sub { $file  = save_edited_file_as ($t) },
                   @button_pref,
                   ) -> pack ( -side => 'left',
                               -anchor => 'w',
                               -padx => 5,
                               -pady => 0,
                               ) ;

    $f -> Label ( -textvariable => \$file ,
                  -background => $bg,
                  ) -> pack( -side => 'left',
                               -anchor => 'w',
                               -padx => 5,
                               -pady => 0,
                               ) ;

    my $m = $t -> menu() ;

    $m -> delete( 'File') ;
    $m -> add( 'separator' ) ;
    $m -> add( 'command',
               -label => 'Cancel/Close',
               -command => [\&hook_run, 'close-current-source-pane'],
               ) ;
    $t ;
}

sub save_edited_file_as {

    my $text = shift ;
    my $file = shift ;
    my $saved ;

    $file ||= find_file ( 1, 'Save File As..' ) or return ;

    my $str =  $text -> get ('1.0' => 'end') ;

    if ( open  FILE, ">$file" ) {
        print FILE $str ;
        close FILE ;
        telthem "saved $file" ;
        $saved = 1 ;
    }
    else {
        telthem "can't write to $file: $!" ;
    }

    # returns:
    # 1. ref to file's contents
    # 2. filename
    # 3. was it saved

    (\$str, $file, $saved ) ;
}


'end'
